package concurrent.util;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * \* User: Administrator
 * <p>
 * \* Date: 2020/11/26
 * <p>
 * \* Time: 10:53
 * <p>
 * \* Description:
 * \
 */
public class CountDownLatchDemo {

    private static CountDownLatch latch = new CountDownLatch(5);

    private static void test() throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                try {
                    System.out.println(Thread.currentThread().getName() + "执行");
                    Thread.sleep(50);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    latch.countDown();
                }
            }).start();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        test();
        latch.await();
        System.out.println("主线程等待");
        System.out.println("执行结束");
    }
}
